package com.example.async;

import com.example.async.service.AsyncService;
import com.example.async.service.TransactionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

/**
 *
 */
@Slf4j
@SpringBootApplication
public class Application {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);

        int pooledSize = ctx.getBeansOfType(ThreadPoolTaskExecutor.class).size();
        log.info("--> pooledSize: {}", pooledSize);

        AsyncService asyncService = ctx.getBean(AsyncService.class);
        asyncService.noReturnMethod();
        String futureValue = asyncService.returnFuture().get();
        log.info("异步返回值{}", futureValue );

        asyncService.returnCompletableFuture()
                .thenAccept(value -> {
                    log.info("异步返回值: {}", value);
                })
                .exceptionally(e -> {
                    log.info("异步调用出错:{}", e.getMessage());
                    e.printStackTrace();
                    return null;
                });

        log.info("---------------");
        asyncService.someOtherMethod();
        asyncService.someOtherMethod2();

        // asyncService.someOtherMethod3();
        log.info("--------------- 测试声明式事务 ----------------");
        TransactionService transactionService = ctx.getBean(TransactionService.class);
        // transactionService.transform("alice", "bob", 100);
        transactionService.notSupported();
    }
}




